import { asyncRoutes, constantRoutes } from '@/router/index'
import { getUserPerm } from '@/api/user'
const state = {
  routes: [],
  userPerm: {}
}

const mutations = {
  setRoutes(state, routes) {
    state.routes = [...constantRoutes, ...routes]
  },
  SET_USER_PERM(state, data) {
    state.userPerm = data
  }
}

const actions = {
  filterRoutes(context, menus) {
    const routes = asyncRoutes.filter(item => {
      return menus.indexOf(item.code) !== -1
    })
    context.commit('setRoutes', routes)
    return routes
  },
  async getUserPermissions(context) {
    const userPerm = await getUserPerm()
    if (userPerm) {
      context.commit('SET_USER_PERM', userPerm.data)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
